package hust.music.util;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.ImageIcon;


import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC;

public class GetMp3Img {
	
	public static void main(String args[]) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {  
		  
        String url = "C:\\陈奕迅-不要说话.mp3";  
        File sourceFile = new File(url);  
        MP3File mp3file = new MP3File(sourceFile);  
          
        AbstractID3v2Tag tag = mp3file.getID3v2Tag();  
        AbstractID3v2Frame frame = (AbstractID3v2Frame) tag.getFrame("APIC");  
        FrameBodyAPIC body = (FrameBodyAPIC) frame.getBody();  
        byte[] imageData = body.getImageData();  
        Image img=Toolkit.getDefaultToolkit().createImage(imageData, 0,imageData.length);  
        System.out.println("img----" + imageData);  
        ImageIcon icon = new ImageIcon(img);              
        FileOutputStream fos = new FileOutputStream("D://test1.jpg");  
        fos.write(imageData);  
        fos.close();  
      
        System.out.println("width:"+icon.getIconWidth());  
        System.out.println("height:"+icon.getIconHeight());  
    }  
}
